#include <stdio.h>
#include <string>
#include <iostream>
#include "Adsorption.h"
#include "GraphBuilder.h"
#include "LabelDistributionTextWriter.h"
#include "LabelDistributionSqlWriter.h"
#include "AdsorptionUtil.h"

int main(int argc, char *argv[])
{
	//AdsorptionUtil::prepareItemData(std::string("D:\\WIN\\Documents\\Visual Studio 2008\\Projects\\C Projects\\Adsorptionc\\data\\MovieLens\\100k_u.item"),
	//	std::string("D:\\WIN\\Documents\\Visual Studio 2008\\Projects\\C Projects\\Adsorptionc\\data\\MovieLens\\100k_u5_base.db3"));
	//return 0;
	std::string sInputFile, sOutputFile;

	LabelDistributionWriter* writer(NULL);
	AdsorptionData *data(NULL);
	int iInputType(0);

	if (argc != 4)
	{
		std::cout << "Usage: " << argv[0] << " <input> <output> <outputType>" << std::endl;
		return -1;
	}

	sInputFile = argv[1];
	sOutputFile = argv[2];
	iInputType = atoi(argv[3]);
	if (iInputType != 1 && iInputType != 2)
	{
		std::cout << "Unsupported output type!" << std::endl;
		return -1;
	}

	// read input data
	data = GraphBuilder::ReadData(sInputFile);
	if (!data)
	{
		std::cout << "Cannot build the view graph!\r\n";
		return -1;
	}

	// process
	Adsorption::Process(*data, 20);

	// prepare the output
	switch(iInputType)
	{
	case 1:
		writer = new LabelDistributionTextWriter(sOutputFile);
		break;
	case 2:
		writer = new LabelDistributionSqlWriter(sOutputFile);
		break;
	default:
		abort();
	}
	
	std::cout << "Writing the output data. Please wait..." << std::endl;
	LabelDistribution &finalDist = data->finalLabelDistributions;
	for (LabelDistribution::iterator itDist(finalDist.begin()),
		itDistEnd(finalDist.end()); itDist != itDistEnd; itDist++)
	{
		writer->Write(itDist->first, itDist->second);
	}

	delete writer;
	delete data;

	std::cout << "We are done. Bye bye..." << std::endl;
	return 0;
}